/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import bean.Loginbean;
import entity.Calendar;
import entity.Event;
import entity.User;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author user
 */
@Stateless

public class Eventejb {

    @PersistenceContext(unitName = "meteocalpersist")
    EntityManager emevent, emuser, emcalendar;

    @Inject
    Calendarejb calejb;

    @Inject
    Loginbean loginbean;

    private User user;
    private Calendar calendar;
    private Event event;
    private Collection<Event> eventCollection;

    public void createEvent(Event event) {
        event.setOrganizer(loginbean.loggedUser());
        emevent.persist(event);
        System.out.println("Event Created Name: " + event.getName());
        user = emuser.find(User.class, loginbean.loggedUser());
        calendar = user.getCalendarID();
        eventCollection = calendar.getEventCollection();
        eventCollection.add(event);
        System.out.println("Event Collection Updated");
        calendar.setEventCollection(eventCollection);
        emcalendar.flush();
        emcalendar.refresh(calendar);
    }

    public void updateEvent(Event eventupdated, String namechange, Date dateeventtochange) throws NoResultException {

        try {
            Query query = emevent.createQuery("SELECT c FROM Event c WHERE c.name = :name AND c.startDateTime = :dateevent").setParameter("name", namechange).setParameter("dateevent", dateeventtochange);
            event = (Event) query.getSingleResult();
            if (event != null && event.getOrganizer().equals(loginbean.loggedUser())) {
                System.out.println("Event to remove : " + event.getName());
                emevent.remove(event);
                user = emuser.find(User.class, loginbean.loggedUser());
                calendar = user.getCalendarID();
                eventCollection = calendar.getEventCollection();
                Iterator itr = eventCollection.iterator();
                event = (Event) itr.next();
                while (!(event.getName().equals(namechange)) && itr.hasNext()) {
                    event = (Event) itr.next();
                }
                eventCollection.remove(event);
                emevent.persist(eventupdated);
                eventCollection.add(eventupdated);
                System.out.println("Event Collection Updated after Updating");
                calendar.setEventCollection(eventCollection);
                emcalendar.flush();
                emcalendar.refresh(calendar);
            }

        } catch (NoResultException e) {
        }

    }

    public void deleteEvent(String eventtodelete, Date dateeventtodelete) throws NoResultException {
        
        try {
            Query query = emevent.createQuery("SELECT c FROM Event c WHERE c.name = :name AND c.startDateTime = :date");
            query.setParameter("name", eventtodelete);
            query.setParameter("date", dateeventtodelete);
            
            event = (Event) query.getSingleResult();
            
            if (event != null && event.getOrganizer().equals(loginbean.loggedUser())) {
                System.out.println("Event to remove : " + event.getName());
                emevent.remove(event);
                user = emuser.find(User.class, loginbean.loggedUser());
                calendar = user.getCalendarID();
                eventCollection = calendar.getEventCollection();
                Iterator itr = eventCollection.iterator();
                event = (Event) itr.next();
                while (!(event.getName().equals(eventtodelete)) && itr.hasNext()) {
                    event = (Event) itr.next();
                }
                eventCollection.remove(event);
                System.out.println("Event Collection Updated after Deleting");
                calendar.setEventCollection(eventCollection);
                emcalendar.flush();
                emcalendar.refresh(calendar);
            }
            
        } catch (NoResultException e) {
            System.out.println("nel catch" );
        }

    }
}
